// packageA/pages/add/add.js
const app = getApp()
import customerApi from "../../../api/index.js"
import hasPermission from "../../../utils/auth"
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		id: '',
		userInfo: '',
		details: {},
		allot_group:false,
		name: '', //string 姓名
		telephone: '', //string 电话
		telephone1:[],
		address: '', //string 地址
		area_id: '', //int 面积id
		hangye_id: '', //int 行业id
		peidian_id: '', //int 配电
		xuqiu_id: '', //int 需求id
		source_id: '', //int 资源id
		huanping_id: '', //int 环评id
		wuye_id: '', //int 物业id
		xiaofang_id: '', //消防id
		big_part_id: '', // 大组id
		create_owner_name: '',
		desc: '', //string 描述
		area: '',
		hangye: '',
		peidian: '',
		big_part: '',
		xuqiu: '',
		source: '',
		huanping: '',
		wuye: '',
		xiaofang: '',
		currentIndex: '',
		area_index: '',
		hangye_index: '',
		peidian_index: '',
		xuqiu_index: '',
		source_index: '',
		big_part_index: '',
		huanping_index: '',
		wuye_index: '',
		xiaofang_index: '',
		showHide: false,
		showSuccess: false,
		isFixed: false,
		selectData: [],
		allData: [],
		wuyeData: [],
		hangyeData: [],
		areaData: [],
		xuqiuData: [],
		peidainData: [],
		xiaofangData: [],
		huanpingData: [],
		sourceData: [],
		bigpartData: [],
		groupData: [],
		title: '',
		group_id:'',
		group:'',
		group_index:'',
	},
	nameInput: function(e) {
		this.setData({
			name: e.detail.value
		})
	},
	telephoneInput: function(e) {
		this.setData({
			telephone: e.detail.value
		})
	},
	addressInput: function(e) {
		this.setData({
			address: e.detail.value
		})
	},
	telephone1Input:function(e){

		this.data.telephone1[e.currentTarget.dataset.index]= e.detail.value
		this.setData({
			telephone1:this.data.telephone1
		})
	},
	descInput: function(e) {
		this.setData({
			desc: e.detail.value
		})
	},
	addMobile: function(e){
		this.data.telephone1.push('')
		this.setData({
			telephone1: this.data.telephone1
		})
	},
	closeMobile:function(e){
		this.data.telephone1.splice(e.currentTarget.dataset.index,1)
		this.setData({
			telephone1: this.data.telephone1
		})
	},
	createOwnerNameInput: function(e) {
		this.setData({
			create_owner_name: e.detail.value
		})
	},
	selectBtn: function(e) {
		let selectData = this.data.selectData
		if (selectData[e.currentTarget.dataset.index] === true) {
			console.log(11)
			selectData[e.currentTarget.dataset.index] = false
			this.setData({
				currentIndex: ''
			})
		} else {
			selectData = []; //改成单选卧槽    
			selectData[e.currentTarget.dataset.index] = true
			console.log('e.currentTarget.dataset.index', e.currentTarget.dataset.index)
			this.setData({
				currentIndex: e.currentTarget.dataset.index
			})
		}
		this.setData({
			selectData: selectData
		})
	},
	submitBtn: function(e) {
		let title = e.currentTarget.dataset.title
		let currentIndex = this.data.currentIndex
		if (title == '物业类型') {
			if (this.data.currentIndex === '') {
				this.setData({
					wuye: '',
					wuye_id: '',
					wuye_index: ''
				})
			} else {
				this.setData({
					wuye: this.data.wuyeData[currentIndex].name,
					wuye_id: this.data.wuyeData[currentIndex].id,
					wuye_index: this.data.currentIndex
				})
			}
		} else if (title == '行业') {
			if (this.data.currentIndex === '') {
				this.setData({
					hangye: '',
					hangye_id: '',
					hangye_index: ''
				})
			} else {
				this.setData({
					hangye: this.data.hangyeData[currentIndex].name,
					hangye_id: this.data.hangyeData[currentIndex].id,
					hangye_index: this.data.currentIndex
				})
			}
		} else if (title == '面积') {
			if (this.data.currentIndex === '') {
				this.setData({
					area: '',
					area_id: '',
					area_index: ''
				})
			} else {
				this.setData({
					area: this.data.areaData[currentIndex].name,
					area_id: this.data.areaData[currentIndex].id,
					area_index: this.data.currentIndex
				})
			}
		} else if (title == '需求') {
			if (this.data.currentIndex === '') {
				this.setData({
					xuqiu: '',
					xuqiu_id: '',
					xuqiu_index: ''
				})
			} else {
				this.setData({
					xuqiu: this.data.xuqiuData[currentIndex].name,
					xuqiu_id: this.data.xuqiuData[currentIndex].id,
					xuqiu_index: this.data.currentIndex
				})
			}
		} else if (title == '配电') {
			if (this.data.currentIndex === '') {
				this.setData({
					peidian: '',
					peidian_id: '',
					peidian_index: ''
				})
			} else {
				this.setData({
					peidian: this.data.peidianData[currentIndex].name,
					peidian_id: this.data.peidianData[currentIndex].id,
					peidian_index: this.data.currentIndex
				})
			}
		} else if (title == '录入组') {
			
			if (this.data.currentIndex === '') {
				this.setData({
					big_part: '',
					big_part_id: '',
					big_part_index: '',
					group:'',
					group_id:'',
					group_index:''
				})
			} else {
				
				this.setData({
					big_part: this.data.bigpartData[currentIndex].name,
					big_part_id: this.data.bigpartData[currentIndex].id,
					big_part_index: this.data.currentIndex,
					group:'',
					group_id:'',
					group_index:''
				})
				if(!this.data.allot_group){//没有分配到组的时候
					customerApi.group({big_part_id:this.data.bigpartData[currentIndex].id}).then(res=>{
						this.setData({
							groupData:res.res,
							group_id:res.res[0].id,
							group:res.res[0].name,
							group_index:0
						})
					})
				}
			}
		} else if (title == '录入小组') {


      
			if (this.data.currentIndex === '') {
				this.setData({
					group: '',
					group_id: '',
					group_index: ''
				})
			} else {
				this.setData({
					group: this.data.groupData[currentIndex].name,
					group_id: this.data.groupData[currentIndex].id,
					group_index: this.data.currentIndex
				})
			}
		} else if (title == '消防') {
			if (this.data.currentIndex === '') {
				this.setData({
					xiaofang: '',
					xiaofang_id: '',
					xiaofang_index: ''
				})
			} else {
				this.setData({
					xiaofang: this.data.xiaofangData[currentIndex].name,
					xiaofang_id: this.data.xiaofangData[currentIndex].id,
					xiaofang_index: this.data.currentIndex
				})
			}
		} else if (title == '环评') {
			if (this.data.currentIndex === '') {
				this.setData({
					huanping: '',
					huanping_id: '',
					huanping_index: ''
				})
			} else {
				this.setData({
					huanping: this.data.huanpingData[currentIndex].name,
					huanping_id: this.data.huanpingData[currentIndex].id,
					huanping_index: this.data.currentIndex
				})
			}
		} else if (title == '来源') {
			if (this.data.currentIndex === '') {
				this.setData({
					source: '',
					source_id: '',
					source_index: ''
				})
			} else {
				this.setData({
					source: this.data.sourceData[currentIndex].name,
					source_id: this.data.sourceData[currentIndex].id,
					source_index: this.data.currentIndex
				})
			}
		}
		this.setData({
			showHide: !this.data.showHide,
			isFixed: !this.data.isFixed,
			title: e.currentTarget.dataset.title
		})
	},
	showBtn: function(e) {
 
 
		let title = e.currentTarget.dataset.title
		if (title == '物业类型') {
			this.setData({
				allData: this.data.wuyeData,
				currentIndex: this.data.wuye_index
			})
		} else if (title == '行业') {
			this.setData({
				allData: this.data.hangyeData,
				currentIndex: this.data.hangye_index
			})
		} else if (title == '面积') {
			this.setData({
				allData: this.data.areaData,
				currentIndex: this.data.area_index
			})
		} else if (title == '需求') {
			this.setData({
				allData: this.data.xuqiuData,
				currentIndex: this.data.xuqiu_index
			})
		} else if (title == '配电') {
			this.setData({
				allData: this.data.peidianData,
				currentIndex: this.data.peidian_index
			})
		} else if (title == '录入组') {
			this.setData({
				allData: this.data.bigpartData,
				currentIndex: this.data.big_part_index
			})
		}else if (title == '录入小组') {
			this.setData({
				allData: this.data.groupData,
				currentIndex: this.data.group_index
			})
		} else if (title == '消防') {
			this.setData({
				allData: this.data.xiaofangData,
				currentIndex: this.data.xiaofang_index
			})
		} else if (title == '环评') {
			this.setData({
				allData: this.data.huanpingData,
				currentIndex: this.data.huanping_index
			})
		} else if (title == '来源') {
			this.setData({
				allData: this.data.sourceData,
				currentIndex: this.data.source_index
			})
		}
		let selectData = this.data.selectData
		selectData = []
		selectData[this.data.currentIndex] = true
		this.setData({
			showHide: !this.data.showHide,
			isFixed: !this.data.isFixed,
			title: e.currentTarget.dataset.title,
			selectData: selectData
		})
		console.log('selectData', this.data.selectData)
	},
	closeBtn: function() {
		this.setData({
			showHide: !this.data.showHide,
			isFixed: !this.data.isFixed
		})
	},
	closeBtn1: function() {
		this.setData({
			showSuccess: !this.data.showSuccess,
			isFixed: !this.data.isFixed
		})
	},
	successBtn: function() {
		if (this.data.name == '') {
			wx.showModal({
				title: '提示',
				content: '姓名不能为空',
				showCancel: false,
				success(res) {
					if (res.confirm) {

					}
				}
			})
				return ;
		} else if (this.data.telephone == '') {
			wx.showModal({
				title: '提示',
				content: '电话不能为空',
				showCancel: false,
				success(res) {
					if (res.confirm) {

					}
				}
			})
			return 
		} else if (this.data.address == '') {
			wx.showModal({
				title: '提示',
				content: '地址不能为空',
				showCancel: false,
				success(res) {
					if (res.confirm) {

					}
				}
			})
			return 
		} else if (this.data.telephone.length != 11) {
			wx.showModal({
				title: '提示',
				content: '电话号码不正常',
				showCancel: false,
				success(res) {
					if (res.confirm) {

					}
				}
			})
			return
		} else if (this.data.big_part == '') {
			wx.showModal({
				title: '提示',
				content: '录入组不能为空',
				showCancel: false,
				success(res) {
					if (res.confirm) {

					}
				}
			})
			return
		} else if (this.data.create_owner_name == '') {
			wx.showModal({
				title: '提示',
				content: '录入人不能为空',
				showCancel: false,
				success(res) {
					if (res.confirm) {

					}
				}
			})
		} else {
      wx.showLoading({
        title: '加载中',
      })

			if(this.data.id){
				let telephone=[]
				telephone.push(this.data.telephone)
				if(this.data.telephone1.length>0){
					for(let i=0;i<this.data.telephone1.length;i++){
						if(this.data.telephone1[i]==''){
							wx.showModal({
								title: '提示',
								content: '电话号码不能为空',
								showCancel: false,
								success(res) {
									if (res.confirm) {
				
									}
								}
							})
							return 
						}
						telephone.push(this.data.telephone1[i])
					}
				}
				customerApi.update({
					name: this.data.name,
					id:this.data.id,
					telephone:telephone,
					address: this.data.address,
					area_id: this.data.area_id,
					hangye_id: this.data.hangye_id,
					peidian_id: this.data.peidian_id,
					xuqiu_id: this.data.xuqiu_id,
					source_id: this.data.source_id,
					huanping_id: this.data.huanping_id,
					wuye_id: this.data.wuye_id,
					xiaofang_id: this.data.xiaofang_id,
					create_owner_name: this.data.create_owner_name,
					big_part_id: this.data.big_part_id,
					desc: this.data.desc
				}).then(res => {
          wx.hideLoading()
					if (res.code == 200) {
						this.setData({
              
							showSuccess: !this.data.showSuccess,
							isFixed: !this.data.isFixed
						})
						setTimeout(() => {
							wx.redirectTo({
								url: '/packageA/pages/allotWrap/detail/detail?customer_id='+this.data.id,
							})
						}, 2000)
					} else if (res.code == 201) {
            wx.hideLoading()
						wx.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success(res) {
								if (res.confirm) {
				
								}
							}
						})
					}
				})
			}else{
				let telephone=[]
				telephone.push(this.data.telephone)
				if(this.data.telephone1.length>0){
					for(let i=0;i<this.data.telephone1.length;i++){
						if(this.data.telephone1[i]==''){
							wx.showModal({
								title: '提示',
								content: '电话号码不能为空',
								showCancel: false,
								success(res) {
									if (res.confirm) {
				
									}
								}
							})
							return 
						}
						telephone.push(this.data.telephone1[i])
					}
				}
				customerApi.submitCustomer({
					name: this.data.name,
					telephone:telephone ,
					address: this.data.address,
					area_id: this.data.area_id,
					hangye_id: this.data.hangye_id,
					peidian_id: this.data.peidian_id,
					xuqiu_id: this.data.xuqiu_id,
					source_id: this.data.source_id,
					huanping_id: this.data.huanping_id,
					wuye_id: this.data.wuye_id,
					xiaofang_id: this.data.xiaofang_id,
					create_owner_name: this.data.create_owner_name,
					big_part_id: this.data.big_part_id,
					group_id: this.data.group_id,
					desc: this.data.desc
				}).then(res => {
					if (res.code == 200) {
						this.setData({
							name: '', //string 姓名
							telephone: '', //string 电话
							address: '', //string 地址
							area_id: '', //int 面积id
							hangye_id: '', //int 行业id
							peidian_id: '', //int 配电
							xuqiu_id: '', //int 需求id
							source_id: '', //int 资源id
							huanping_id: '', //int 环评id
							wuye_id: '', //int 物业id
							xiaofang_id: '', //消防id
							desc: '', //string 描述
							area: '',
							hangye: '',
							peidian: '',
							xuqiu: '',
							source: '',
							huanping: '',
							wuye: '',
							xiaofang: '',
							currentIndex: '',
							area_index: '',
							hangye_index: '',
							peidian_index: '',
							xuqiu_index: '',
							source_index: '',
							huanping_index: '',
							wuye_index: '',
							xiaofang_index: '',
							showSuccess: !this.data.showSuccess,
							isFixed: !this.data.isFixed
						})
            wx.hideLoading()
						setTimeout(() => {
							wx.redirectTo({
								url: '/packageA/pages/allotWrap/allot/allot',
							})
						}, 1500)
					} else if (res.code == 201) {
            wx.hideLoading()
						wx.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success(res) {
								if (res.confirm) {
				
								}
							}
						})
					}
				})
			}
	
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		if (options.id != undefined) {
			this.setData({
				id: options.id
			})

			wx.setNavigationBarTitle({
				title: '编辑客户'
			})
		}
		app.CheckSession().then(res => {
			if (res == true) {
				app.GetUser().then(data => {
					this.setData({
						userInfo: data,
						create_owner_name: data.name,
						allot_group:hasPermission('allot_group')
					})
					if (options.id != undefined) {
						customerApi.getCustomerDetails({
							customer_id: options.id
						}).then(res => {
							if (res.code == 200) {
							let telephone=res.res.mobiles[0]
							res.res.mobiles.splice(0,1)
							let telephone1=res.res.mobiles
								this.setData({
									details: res.res,
									name: res.res.name,
									telephone:telephone,
									telephone1:res.res.mobiles,
									address: res.res.address,
									create_owner_name: res.res.create_owner_name
								})
						
								customerApi.wuye().then(res => {
									this.setData({
										wuyeData: res.res
									})
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.wuye_id
										})
										if (index != -1) {
											this.setData({
												wuye_id: this.data.details.wuye_id,
												wuye_index: index,
												wuye: this.data.details.wuye
											})
										}else{
											this.setData({
										
												wuye: this.data.details.wuye
											})
										}
									}
								})
						
								customerApi.hangye().then(res => {
									console.log('res', res)
									this.setData({
										hangyeData: res.res
									})
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.hangye_id
										})
										if (index != -1) {
											this.setData({
												hangye_id: this.data.details.hangye_id,
												hangye_index: index,
												hangye: this.data.details.hangye
											})
										}else{
											this.setData({
										
												hangye: this.data.details.hangye
											})
										}
									}
								})
								customerApi.area().then(res => {
									this.setData({
										areaData: res.res
									})
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.area_id
										})
										if (index != -1) {
											this.setData({
												area_id: this.data.details.area_id,
												area_index: index,
												area: this.data.details.area
											})
										}else{
											this.setData({
										
												area: this.data.details.area
											})
										}
									}
								})
								customerApi.bigpart().then(res => {
									this.setData({
										bigpartData: res.res
									})
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.big_part_id
										})
										if (index != -1) {
											this.setData({
												big_part_id: this.data.details.big_part_id,
												big_part_index: index,
												big_part: this.data.details.big_part
											})
											if(!this.data.allot_group){//没有分配到组的时候
												customerApi.group({big_part_id:this.data.bigpartData[currentIndex].id}).then(res=>{
													let index = res.res.findIndex(item => {
														return item.id == this.data.details.group_id
													})
													if (index != -1) {
														this.setData({
															groupData:res.res,
															group_id:this.data.details.group_id,
															group:res.res[index].name,
															group_index:index
														})
													}else{
														this.setData({
															groupData:res.res,
															group_id:res.res[0].id,
															group:res.res[0].name,
															group_index:0
														})
													}
												})
											}
											
											
										}
									}
									
								})
								customerApi.xuqiu().then(res => {
									console.log('res', res)
									this.setData({
										xuqiuData: res.res
									})
									
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.xuqiu_id
										})
										if (index != -1) {
											this.setData({
												xuqiu_id: this.data.details.xuqiu_id,
												xuqiu_index: index,
												xuqiu: this.data.details.xuqiu
											})
										}
									}else{
										this.setData({
										
											xuqiu: this.data.details.xuqiu
										})
									}
								})
								customerApi.peidian().then(res => {
									this.setData({
										peidianData: res.res
									})
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.peidian_id
										})
										if (index != -1) {
											this.setData({
												peidian_id: this.data.details.peidian_id,
												peidian_index: index,
												peidian: this.data.details.peidian
											})
										}else{
											this.setData({
											
												peidian: this.data.details.peidian
											})
										}
									}
								})
								customerApi.xiaofang().then(res => {
									console.log('res', res)
									this.setData({
										xiaofangData: res.res
									})
									
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.xiaofang_id
										})
										if (index != -1) {
											this.setData({
												xiaofang_id: this.data.details.xiaofang_id,
												xiaofang_index: index,
												xiaofang: this.data.details.xiaofang
											})
										}else{
											this.setData({
											
												xiaofang: this.data.details.xiaofang
											})
										}
									}
								})
								customerApi.huanping().then(res => {
									this.setData({
										huanpingData: res.res
									})
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.huanping_id
										})
										if (index != -1) {
											this.setData({
												huanping_id: this.data.details.huanping_id,
												huanping_index: index,
												huanping: this.data.details.huanping
											})
										}else{
											this.setData({
											
												huanping: this.data.details.huanping
											})
										}
									}
								})
								customerApi.source().then(res => {
									console.log('res', res)
									this.setData({
										sourceData: res.res
									})
									
									if (this.data.id) {
										let index = res.res.findIndex(item => {
											return item.id == this.data.details.source_id
										})
										if (index != -1) {
											this.setData({
												source_id: this.data.details.source_id,
												source_index: index,
												source: this.data.details.source
											})
										}else{
											this.setData({
											
												source: this.data.details.source
											})
										}
									}
								})



							}
						})
					} else {


						customerApi.wuye().then(res => {
							this.setData({
								wuyeData: res.res
							})

						})
						customerApi.hangye().then(res => {
							console.log('res', res)
							this.setData({
								hangyeData: res.res
							})
						})
						customerApi.area().then(res => {
							this.setData({
								areaData: res.res
							})
						})
						customerApi.bigpart().then(res => {
							this.setData({
								bigpartData: res.res
							})
						})
						customerApi.xuqiu().then(res => {
							console.log('res', res)
							this.setData({
								xuqiuData: res.res
							})
						})
						customerApi.peidian().then(res => {
							this.setData({
								peidianData: res.res
							})
						})
						customerApi.xiaofang().then(res => {
							console.log('res', res)
							this.setData({
								xiaofangData: res.res
							})
						})
						customerApi.huanping().then(res => {
							this.setData({
								huanpingData: res.res
							})
						})
						customerApi.source().then(res => {
							console.log('res', res)
							this.setData({
								sourceData: res.res
							})
						})
					}
				})
			}
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},





})
